package algorithm.binarySearch;
/*
二分查找算法（非递归版）
 */
public class BinarySearch {
  public static void main(String[] args) {
    int[]arr = new int[]{1,3,8,10,11,67,100};
    int index = binarySearch(arr, 67);
    System.out.println("index=" + index);
  }

  public static int binarySearch(int[]arr,int target){

    int left = 0;
    int right = arr.length - 1;
    while (left <= right){
      int mid = (left + right)/2;
      if (target == arr[mid]){
        return mid;
      }else if (target > arr[mid]){
        left = mid + 1;
      }else if (target < arr[mid]){
        right = mid - 1;
      }
    }
    return -1;
  }
}
